package com.alignet.gn.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UtilFormat {

	private static final Logger log = LoggerFactory.getLogger(UtilFormat.class);
	private static StringBuilder stbf;

	static {
		if (stbf == null) {
			stbf = new StringBuilder();
		}

	}

	public static String getDateFormat(Timestamp time) {
        SimpleDateFormat standar;
        String standarFormat="";
        standar = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        standarFormat = standar.format( new Date(time.getTime()) );
	return standarFormat;
	}
	 
    public static String getCurrentDate(){
        TimeZone tz = TimeZone.getTimeZone("GMT-5:00");
        Date date = Calendar.getInstance().getTime();
        SimpleDateFormat date_format_gmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        date_format_gmt.setTimeZone(tz);
        //Timestamp t = new Timestamp(date);
        return date_format_gmt.format(date);
    }	
    public static Timestamp getTime() {
    	
		Calendar hoy = Calendar.getInstance();
		hoy.add(Calendar.HOUR, -5);
		System.out.println(hoy.getTime());
		Timestamp t = new Timestamp(hoy.getTime().getTime());
		return t;
	}
    
	public static void main(String[] args) {
			System.out.println(getTime());
	    
	}
	public static String panFormat(final String bin, final String lastPan) {
		stbf.setLength(0);
		if(bin==null || bin.length()== 0){
			return "";
			}else{
		return stbf.append(bin).append("******").append(lastPan).toString();
			}
	}
	

	public static String panFormat(final String card) {
		stbf.setLength(0);
		return stbf.append(card.substring(0, 6)).append("******").append(card.substring(12, 16)).toString();
	}	

	public static String getPanLast(final String pan) {
		stbf.setLength(0);
		return stbf.append(StringUtils.reverse( StringUtils.reverse(pan).substring(0, 4).toString()  )).toString() ;
	}
/*
	public static String getConvertDecimalToIntegerc(final String number, final int exp) {

		final int ind = number.indexOf('.');
		String num;
		if (ind == -1) {
			num = number.concat(StringUtils.rightPad("", exp, '0'));
		} else {
			final String expo = StringUtils.substring(number, ind+1);

			System.out.println("expox : "+expo);
			System.out.println(StringUtils.rightPad(expo, exp, 'x'));
			System.out.println(StringUtils.substring(number, 0, ind));
			num = StringUtils.substring(number, 0, ind).concat(StringUtils.rightPad(expo, exp, 'x'));
		}
		return num;
	}

	public static String getConvertLongToDecimalx(final String number, final int exp) {
		stbf.setLength(0);
		final int indice = number.indexOf('.');

		if (indice == -1) {
			if (number.length() >= exp) {
				final String res = stbf.append(number).reverse().insert(exp, '.').reverse().toString();
				if(res.charAt(0)=='.'){ stbf.insert(0, "0");}
			} else {
				stbf.append("0.").append(StringUtils.leftPad(number, exp, "0"));
			}
		}else{
			stbf.append(number);
		}

		return stbf.toString();
	}
*/
	//*****************************************

	public static long getConvertDecimalToIntegerLong(final String number, final int exp, final boolean round) {
		return Long.valueOf(getIntegerValueOf(number, exp, round)).longValue();
	}

	public static String getIntegerValueOfInput(String valor, final int exp, final boolean round) {
		String x = "";

		if(valor.contains(".")){
			if (round) {
				x = String.valueOf(BigDecimal.valueOf(Double.valueOf(valor)).setScale(2, BigDecimal.ROUND_HALF_UP).movePointRight(exp));
			} else {
				x = String.valueOf(BigDecimal.valueOf(Double.valueOf(valor)).movePointRight(exp));
			}

		} else {
			x.concat(StringUtils.rightPad("", 2, '0'));
		}

		return x;
	}
/**
 * Conversion de numero Decimal a Entero
 */
	public static String getIntegerValueOf(String valor, final int exp, final boolean round) {
		String res = "";

		try {
			if(valor.contains(".")){
				if (round) {
					res = String.valueOf(BigDecimal.valueOf(Double.valueOf(valor)).setScale(2, BigDecimal.ROUND_HALF_UP).movePointRight(exp));
				} else {
					res = String.valueOf(BigDecimal.valueOf(Double.valueOf(valor)).movePointRight(exp));
				}
			} else {
				res = valor.concat(StringUtils.rightPad("", 2, '0'));
			}
		} catch (NumberFormatException e) {
			log.error("[ getIntegerValueOf ] ",e);
		}

		log.debug("[ getIntegerValueOf ] valor: {}, resultado: {}", valor, res);

		return res;
	}

	/**
	 * Conversion de numero Entero a Decimal
	 * @param valor
	 * @param exp
	 * @return
	 */
	public static String getDecimalValueOf(String valor, final int exp) {

		String res = "";
		try {
			if(valor==null){valor = "0";}
			
			if (valor.contains(".")) {
				res = getDecimalFormat(valor);
			} else {
				res = getDecimalFormat(String.valueOf(BigDecimal.valueOf(Long.valueOf(valor)).movePointLeft(exp)));
			}
		} catch (NumberFormatException e) {
			log.error("[ getDecimalValueOf ] ", e);
		}

		log.debug("[ getIntegerValueOf ] valor: {}, resultado: {}", valor, res);

		return res;
	}

	/**
	 * Formateador decimal de numeros
	 * @param valor
	 * @return
	 */
	private static String getDecimalFormat(final String valor) {
		final DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
		formatSymbols.setDecimalSeparator('.');
		formatSymbols.setGroupingSeparator(' ');
		final String decimalFormat="###0.00";
/*
		try {
			decimalFormat = GNUtils.getMessage("format.decimal");
		} catch (Exception e) {}
*/

		final DecimalFormat formatter = new DecimalFormat(decimalFormat, formatSymbols);

		return formatter.format(Double.valueOf(valor));

	}

	public static long convertTypeChange(final long totalPurchase, final String typeChange) {

		String type = (BigDecimal.valueOf(
				Double.valueOf(UtilFormat.getDecimalValueOf(typeChange, 2))).multiply(
				new BigDecimal(UtilFormat.getDecimalValueOf(totalPurchase
						+ "", 2)))).setScale(2, BigDecimal.ROUND_HALF_UP).toString();

		final long total = UtilFormat.getConvertDecimalToIntegerLong(type, 2, false);

		return total;
	}

	public static BigDecimal getInterest(final String tea, final long n,final String k) {

		final float interes = (float) (Float.valueOf(tea) / 100) + 1;

		return BigDecimal.valueOf(((Math.pow((interes), (n / ((float) 360)))) - 1) * Double.valueOf(k));
	}



}
